const app = getApp();
Page({
    data:{
        data: [],
        page:1,
        loadData:false,
        empty:true,
        end:false
    },
    onLoad(){
        this.getData(this.data.page)
    },
    onPullDownRefresh (){
        this.setData({
            page:1,
            data:[],
            empty:true,
            end:false
        })
        this.getData(this.data.page)
        wx.stopPullDownRefresh()
    },
    onReachBottom(){
        if (!end){
            this.setData({
                page:this.data.page+1
            })
            this.getData(this.data.page)
        }
    },
    getData(page){
        if (!this.data.loadData){
            this.setData({
                loadData:true
            })
            app.axios('/notices','GET',response=>{
                if (this.data.page>1) {
                    if (response.length>0) {
                        this.setData({
                            data: [...this.data.data, ...response],
                            empty:false
                        })
                        if (response.length<10){
                            this.setData({
                                end:true
                            })
                        }
                    }else {
                        this.setData({
                            end:true
                        })
                    }
                }else {
                    if (response.length>0){
                        this.setData({
                            data: [...this.data.data, ...response],
                            empty:false
                        })
                        if (response.length<10){
                            this.setData({
                                end:true
                            })
                        }
                    }
                }
                this.setData({
                    loadData:false
                })
            },{page:page})
        }

    }
});
